﻿/**********************************************************************
/// <summary>
// Name of File     :   LoginPresenter.cs
// Author           :   Parminder Singh
// Create date      :   12th Nov 2011
/// </summary>
************************************************************************/

using System.Collections.Generic;
using Crystal.Common.Model;
using System;
using CrystalCommon.BLL;
using Crystal.CLL;

namespace CrystalCommon.Presenter
{
    public class LoginPresenter : ILoginPresenter
    {
        #region Fields

        private ILoginView view;
        private CrystalEntities context;

        #endregion

        #region Constructors

        public LoginPresenter(ILoginView view)
        {
            this.view = view;
            this.view.CheckUserName += view_CheckUserCredentials;
            //this.view.CheckUserCredentials += view_CheckUserCredentials;
            context = new CrystalEntities(CommonCLL.mstrContextConnectionstring);
            
        }

        #endregion

        #region Event Handlers

        public bool view_CheckUserCredentials(string username, string password)
        {
            using (LoginManager clsLoginManager = new LoginManager())
            {
                List<USR_Users> users = clsLoginManager.CheckLoginCredentials(context, username, password);
                if (users.Count > 0)
                {
                    CommonCLL.UserID = users[0].UserID;
                    CommonCLL.UserName = users[0].UserName;
                    CommonCLL.UserFullName = users[0].FirstName +" "+users[0].LastName;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        //public bool view_CheckUserCredentials(string username, string password)
        //{
        //    using (LoginManager clsLoginManager = new LoginManager())
        //    {
        //        List<USR_Users> users = clsLoginManager.CheckLoginCredentials(context, username, password);
        //        if (users.Count > 0)
        //        {
        //            return true;
        //        }
        //        else
        //        {
        //            return false;
        //        }
        //    }
        //}

        #endregion
    }
}
